﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
using System.Text;
namespace Web
{
    public partial class AdminExamRoom : System.Web.UI.Page
    {
        public static Module_Page Pages = new Module_Page();
        private string _ExamRoomName = "";
        private DateTime? _StartDate = null,_EndDate = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterExamroom"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (Request.QueryString["page"] != null && Request.QueryString["page"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["page"]))
                {
                    Pages.PageNum = int.Parse(Request.QueryString["page"]);
                }
            }
            if (PageValidate.QueryStringIsEmpty("ExamRoomName"))
            {
                _ExamRoomName = "";
            }
            else
            {
                _ExamRoomName = PageValidate.QueryString("ExamRoomName");
            }
            if (PageValidate.QueryStringIsEmpty("StartDate"))
            {
                _StartDate = null;
            }
            else
            {
                _StartDate = DateTime.Parse(PageValidate.QueryString("StartDate"));
            }
            if (PageValidate.QueryStringIsEmpty("EndDate"))
            {
                _EndDate = null;
            }
            else
            {
                _EndDate = DateTime.Parse(PageValidate.QueryString("EndDate"));
            }
            if (!IsPostBack)
            {
                BindExamRoom();
                BindSchool();
            }
        }

        protected void BindSchool()
        {
            IList<SchoolInfo> schoolInfos = new List<SchoolInfo>();
            schoolInfos = School.GetList();
            this.dpSchoolList.DataSource = schoolInfos;
            this.dpSchoolList.DataTextField = "Name";
            this.dpSchoolList.DataValueField = "Id";
            this.dpSchoolList.DataBind();
        }

        protected void BindExamRoom()
        {
            Pages.AllCount = ExamRoom.GetCount(_ExamRoomName, _StartDate, _EndDate);
            Pages.PageSize = 20;
            Pages.PageName = GetPageName();
            Pages.Compute();
            this.PageLabel.Text = Pages.GetPage();
            IList<ExamRoomInfo> list = ExamRoom.GetList(_ExamRoomName, _StartDate, _EndDate, Pages.First, Pages.Max);
            this.ExamRoomList.DataSource = list;
            this.ExamRoomList.DataBind();
        }

        private string GetPageName()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("AdminExamRoom.aspx?id=1");
            if (!PageValidate.IsEmpty(this._ExamRoomName))
            {
                builder.Append("&ExamRoomName=");
                builder.Append(this._ExamRoomName);
            }
            if (_StartDate != null)
            {
                builder.Append("&StartDate=");
                builder.Append(this._StartDate);
            }
            if (_EndDate != null)
            {
                builder.Append("&EndDate=");
                builder.Append(this._EndDate);
            }
            builder.Append("&Page=");
            return builder.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (PageValidate.FormIsEmpty("ExamRoomName"))
            {
                this._ExamRoomName = "";
            }
            else
            {
                this._ExamRoomName = PageValidate.Form("ExamRoomName");
            }
            if (PageValidate.FormIsEmpty("StartDate"))
            {
                this._StartDate = null;
            }
            else
            {
                this._StartDate = DateTime.Parse(PageValidate.Form("StartDate"));
            }
            if (PageValidate.FormIsEmpty("EndDate"))
            {
                this._EndDate = null;
            }
            else
            {
                this._EndDate = DateTime.Parse(PageValidate.Form("EndDate"));
            }
            this.BindExamRoom();
        }
    }
}
